package com.wk.springboot.httpUtils;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.assertj.core.util.Preconditions;

/**
 * @Classname DrspHttpMethod
 * @Description DrspHttpMethod
 * @Date 2020/8/7 11:15
 * @Author by weikai
 */
public abstract class DrspHttpMethod {

    /**
     * 默认字符集
     */
    static String CHARSET_UTF8_STR = "UTF-8";

    /**
     * 默认字符集
     */
    static Charset CHARSET_UTF8 = Charset.forName(CHARSET_UTF8_STR);

    CloseableHttpClient httpClient;

    String url;

    String contentType;

    Map<String, String> urlParams = new LinkedHashMap<String, String>();

    Map<String, String> header = new HashMap<String, String>();

    public DrspHttpMethod(CloseableHttpClient httpClient) {
        this.httpClient = httpClient;
    }

    public DrspHttpMethod setUrlParams(Map<String, String> urlParams) throws ParseException, IOException {
        if (MapUtils.isNotEmpty(urlParams)) {
            this.urlParams.putAll(urlParams);
            this.url = buildGetUrl(this.url, this.urlParams);
        }
        return this;
    }

    public DrspHttpMethod setHeader(Map<String, String> header) {
        if (MapUtils.isNotEmpty(header)) {
            this.header.putAll(header);
        }
        return this;
    }

    /**
     * 构造get请求的url地址
     * @param url
     * @param urlParams
     * @return
     * @throws ParseException 
     * @throws IOException
     */
    public String buildGetUrl(String url, Map<String, String> urlParams) throws ParseException, IOException {

        checkUrl(url);

        List<NameValuePair> pairs = buildNameValueParams(urlParams);

        if (CollectionUtils.isNotEmpty(pairs)) {
            url += "?" + EntityUtils.toString(new UrlEncodedFormEntity(pairs, CHARSET_UTF8));
        }
        return url;
    }

    /**
     * 验证url
     * @param url
     */
    private void checkUrl(String url) {
        Preconditions.checkArgument(StringUtils.isNotBlank(url), "http调用时url为空");
    }

    /**
     * 构造NameValueParams
     * @param urlParams
     * @return
     */
    private List<NameValuePair> buildNameValueParams(Map<String, String> urlParams) {
        List<NameValuePair> pairs = new ArrayList<NameValuePair>(urlParams.size());

        for (Map.Entry<String, String> entry : urlParams.entrySet()) {
            pairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
        }
        return pairs;
    }

    public abstract void releaseConnection();

}
